#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
const int N = 1e6 + 10, M = 2 * N;
int h[N], e[M], ne[M], idx;
int f[N], cnt;
int w[N];
int n, sum;
vector<int>ans;
void add(int a, int b)
{
	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u, int fa)
{
	f[u] = w[u];
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j != fa) {
			dfs(j, u);
			f[u] += f[j];
		}
	}
	if (f[u] == sum)cnt++, ans.push_back(u), f[u] = 0;
}
int main()
{
	cin >> n;
	int root = 0;
	memset(h, -1, sizeof(h));
	for (int i = 1; i <= n; i++) {
		int x;
		scanf("%d", &x);
		if (!x)root = i;
		scanf("%d", &w[i]);
		add(i, x);
		add(x, i);
		sum += w[i];
	}
	if (sum % 3 != 0) {
		cout << -1 << endl;
		return 0;
	}
	sum /= 3;
	dfs(root, 0);
	// 	for(int i=1;i<=n;i++){
	// 	    cout<<" i=="<<i<<"    f"<<"=="<<f[i]<<endl;
	// 	}
	if (cnt <= 2) {
		cout << -1 << endl;
		return 0;
	}
	for (int i = 0; i < 2; i++) {
		cout << ans[i] << " ";
	}
	return 0;
}